package org.lucky.controller;

import cn.shuibo.annotation.Decrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.lucky.service.impl.LuckyServiceImpl;
import org.lucky_common.domain.LuckyShare;
import org.lucky_common.domain.ParseParamObj;
import org.lucky_common.dto.SetFirstStageDto;
import org.lucky_common.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class LuckyShareController {

    @Autowired
    private LuckyServiceImpl luckyService;

    @PutMapping("/generateShare")
    //    @Encrypt
    @Decrypt
    public AjaxResult handleShare(@RequestBody ParseParamObj parseParamObj) {
        LuckyShare luckyShare = luckyService.generateUniqueShareToken(parseParamObj);
//        return AjaxResult.success("https://chen66.top?ref=" + token);
        return AjaxResult.success(luckyShare);
    }

    //---------------------------------------------------------------

    @PutMapping("/update2")
    //    @Encrypt
    @Decrypt
    public AjaxResult updateByCode2(@RequestBody ParseParamObj parseParamObj){
        System.out.println(parseParamObj);
        LuckyShare inviteCode = luckyService.updateByCode2(parseParamObj);
        return AjaxResult.success(inviteCode);
    }


    @PutMapping("/setFirstStage")
    @Decrypt
    public AjaxResult setFirstStage(@RequestBody SetFirstStageDto setFirstStageDto){
        System.out.println(setFirstStageDto);

        LuckyShare inviteCode = luckyService.getOne(new QueryWrapper<LuckyShare>().eq("inviteCode", setFirstStageDto.getRef()));
        if (inviteCode != null){
            inviteCode.setFirstStage(true);
            luckyService.updateById(inviteCode);
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @PutMapping("/getInviteCodeInfo")
    @Decrypt
    public AjaxResult getInviteCodeInfo(@RequestBody ParseParamObj parseParamObj){
        System.out.println(parseParamObj);
        LuckyShare inviteCode = luckyService.getOne(new QueryWrapper<LuckyShare>().eq("inviteCode", parseParamObj.getRef()));
        if (inviteCode != null){
            return AjaxResult.success(inviteCode);
        }else {
            return AjaxResult.error();
        }
    }

    @PutMapping("/upCount")
    //    @Encrypt
    @Decrypt
    public AjaxResult upCount(@RequestBody ParseParamObj parseParamObj) {
        String s = luckyService.upCount(parseParamObj);
        return AjaxResult.success(s);
    }

    @PutMapping("/upCount2")
    //    @Encrypt
    @Decrypt
    public AjaxResult upCount2(@RequestBody ParseParamObj parseParamObj) {
        String s = luckyService.upCount2(parseParamObj);
        return AjaxResult.success(s);
    }


    @PostMapping("/recordClick2")
    //    @Encrypt
    @Decrypt
    public AjaxResult recordClick2(@RequestBody ParseParamObj parseParamObj) {
        System.out.println(parseParamObj);
        String s = luckyService.recordValidClick2(parseParamObj);
        return AjaxResult.success(s);
    }

    @PutMapping("/check")
    public AjaxResult check(@RequestBody ParseParamObj parseParamObj){
        return luckyService.check(parseParamObj);
    }

    @PutMapping("/flushedCount2")
    //    @Encrypt
    @Decrypt
    public AjaxResult flushedCount2(@RequestBody ParseParamObj parseParamObj){
        System.out.println(parseParamObj);
        LuckyShare inviteCode = luckyService.flushCount2(parseParamObj);
        return AjaxResult.success(inviteCode);
    }

    @PostMapping("/recordClick3")
    //    @Encrypt
    @Decrypt
    public AjaxResult recordClick3(@RequestBody ParseParamObj parseParamObj) {
        System.out.println(parseParamObj);
        String s = luckyService.recordValidClick3(parseParamObj);
        return AjaxResult.success(s);
    }

    @PutMapping("/check2")
    public AjaxResult check2(@RequestBody ParseParamObj parseParamObj){
        return luckyService.check2(parseParamObj);
    }
}